var picTab =  { 
  z: 0, //for setting the initial z-index's
  inAnimation:false, //flag for testing if we are in a animation
  
  initPics:function(tab){
	  picTab.z = 0;
	  tab.find('#pictures>img').each(function() { //set the initial z-index's
		    picTab.z++; //at the end we have the highest z-index value stored in the z variable
		    $(this).css('z-index', picTab.z); //apply increased z-index to <img>		    
	  });  
  },
  

  swapFirstLast: function(tab, isFirst) {	  
    if(picTab.inAnimation) return false; //if already swapping pictures just return
    else picTab.inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = picTab.z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = picTab.z; inDeCrease = -1; } //set variables for "previous" action
    
    tab.find('#pictures>img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              picTab.inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; 
  }
  
};